// pages/order/getOrder/getOrder.js
const handler = require("../../../utils/function.js");
var app = getApp(),
  api = require("../../../api.js");
// 播放器
// const innerAudioContext = wx.createInnerAudioContext();
// innerAudioContext.obeyMuteSwitch = false;
/**
 * 录音播放
 * @param src 音频地址
 * @param duration 音频秒数（单位 S）
 */
// let SoundRecordingPlay = function ({ src, duration }) {
//   let _src = src;
//   let _duration = duration;
//   if (_src && _duration) {
//     innerAudioContext.src = _src
//     innerAudioContext.play();
//     // setTimeout(function () {
//     //   innerAudioContext.stop();
//     // }, _duration * 1000)
//   }
// }
Page({
  /**
   * 页面的初始数据
   */
  data: {
    img: handler.driverImg,
    time: 20,
    order: [],
    tabid:0,
    latitude: '',
    longitude: '',
    speed: '',
    accuracy: '',
    is_stop: 0,
    imgs: handler.userImg,
    get_order_timer: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  tab:function(e){
    var that =this,tabid = e.currentTarget.dataset.id;
    
    that.setData({
      tabid:tabid
    })
    that.getone()
  },
  onShow:function(){
    wx.getStorageSync("like")
    this.getone(wx.getStorageSync("like"));
  },
  onLoad: function (options) {
    var iscle = 0;
    // this.timeTsk(iscle)
    if (options.like) {
    
      app.like = options.like;
      // wx.getStorageSync("like")
      // this.getone(wx.getStorageSync("like"));
    }
    // that.setorder()
    //   let get_order_timer = setInterval(() => {
    //       this.setData({
    //           time:(this.data.time-1)
    //       })

    //       if(this.data.time==0){
    //    
    //           this.getone(wx.getStorageSync("like"));
    //           this.setData({
    //               time:10
    //           })
    //       }

    //   }, 1000);
    //   this.setData({
    //       get_order_timer
    //   })
    // 获取优惠券
    if (options.uid) {
      app.shareCouponsMessage(options.uid)
    }
    //获取订单状态

  },
  m_refuse:function(e){
  
    var that = this,order_no = e.currentTarget.dataset.sn;
    app.request({
      url: api.order.refuseOrder,
      method: 'post',
      data: {
        order_no: order_no
      },
      success: (res) => {
        clearInterval(that.data.get_order_timer);
        that.setData({
          time: 0
        })
        wx.showLoading({
          title: '拒单中',
        })
        that.getone(wx.getStorageSync("like"));
      }
    })
  },
  //拒单
  refuse: function (e) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否拒绝此单',
      success(res) {
        if (res.confirm) {
          that.m_refuse(e)
        } else if (res.cancel) {

        }
      }
    })

  },
  //定时任务
  setorder: function () {
    let that = this;

  },
  getone: function (e) {
    const that = this;

    wx.getLocation({
      type: 'gcj02',
      success: function (res) {

        app.request({
          url: api.order.getOrder,
          method: 'post',
          data: {
            tabid:that.data.tabid,
            latitude: res.latitude,
            longitude: res.longitude,
            speed: res.speed,
            accuracy: res.accuracy,
            like: e
          },
          success: function (rs) {

            wx.hideLoading()
            clearInterval(that.data.get_order_timer);
            if (rs.code == 0) {
              wx.showToast({
                title: rs.msg,
                icon: 'none',
              })
              wx.reLaunch({
                url: '../../index/index'
              })
            } else {
              wx.setStorageSync("is_stop", 0)
              that.setData({
                order: rs.data,
              })
              let get_order_timer = setInterval(() => {
                
                if (that.data.time == 0) {
                  // that.m_refuse()
                  that.getone(wx.getStorageSync("like"));
                  that.setData({
                    time: 20
                  })
                } else {
                  that.setData({
                    time: (that.data.time - 1)
                  })
                }

              }, 3000);
              that.setData({
                get_order_timer
              })
              // 语音提示
              // if(rs.data.length==1){
              //   new SoundRecordingPlay({ src: '/we7/resource/video/alert.mp3', duration: "14" });
              // }
              //  clearTimeout(that.timeTsk);
            }
          }
        })
      }
    })


  },
  // 倒计时
  timeTsk: function () {
    const time = setInterval(() => {
      if (this.data.time > 0) {
        this.setData({
          time: --this.data.time
        });
      } else {
        if (wx.getStorageSync("is_stop") == 1) {
          clearTimeout(time);
        } else {
          this.getone();
          this.setData({
            time: 10
          });
        }
      }
    }, 1000);
  },
  onUnload() {
    clearInterval(this.data.get_order_timer);
  },
  // intoPageHandler 页面跳转
  intoPageHandler: function (e) {
    
    clearInterval(this.data.get_order_timer);
    var that = this;
    if (!that.data.order.length){
      wx.showToast({
        title: '订单不存在',
        icon:'none'
      })
      return;
    }
   
    let orderNo = e.currentTarget.dataset.sn;  //order.order_no;
    // 调取方法 实时发送司机位置
    app.uploadLocation(orderNo);
    // 跳转至订单页面
    let skip = "../orderedStatus/orderedStatus?order_no="+orderNo;
    wx.showLoading({
      title: '',
    })
    app.request({
      url: api.order.grabsOrder,
      method: 'post',
      data: {
        order_no: orderNo,
        formid: null
      },
      success: function (rs) {
        wx.hideLoading()
        wx.setStorageSync("is_stop", 1)
        if (rs.code == 1) {
          wx.setStorageSync("current_order", this.data.order)
          // wx.redirectTo({
          //   url: e.currentTarget.dataset.skip,
          // })
          app.gateway(orderNo, 1)
          if (wx.canIUse('requestSubscribeMessage')) {
            app.request({
              url: api.message.getTmplIds,
              method: "GET",
              data: {
                type: 2
              },
              success: res => {
                wx.requestSubscribeMessage({
                  tmplIds: res.data,
                  complete: res => {
                 
                  }
                })
              }
            })
          }
          if (that.data.order[0].is_pre == 1) {
            handler.intoPageHandler(skip)

          } else {
            wx.redirectTo({
              url: '/pages/driverOrder/order/order',
            })
          }

        } else {
          wx.showToast({
            title: rs.msg,
            duration: 1000
          })
        }
      }
    })
  
  },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;

    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})